package org.NooLab.nativebrowser.app.controller.parser;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

/**
 * <p>
 * Hello <b>there</b> now!
 * </p>
 * , p.ownText() returns "Hello now!", whereas p.text() returns
 * "Hello there now!". Note that the text within the b element is not returned,
 * as it is not a direct child of the p element. So, this should do: String
 * ownText = div.ownText();
 * 
 * -> remove tags: span, b , em ,
 * 
 * 
 * http://jsoup.org/cookbook/extracting-data/selector-syntax
 * 
 * Selector overview
 * 
 * tagname: find elements by tag, e.g. a ns|tag: find elements by tag in a
 * namespace, e.g. fb|name finds <fb:name> elements #id: find elements by ID,
 * e.g. #logo .class: find elements by class name, e.g. .masthead [attribute]:
 * elements with attribute, e.g. [href] [^attr]: elements with an attribute name
 * prefix, e.g. [^data-] finds elements with HTML5 dataset attributes
 * [attr=value]: elements with attribute value, e.g. [width=500] [attr^=value],
 * [attr$=value], [attr*=value]: elements with attributes that start with, end
 * with, or contain the value, e.g. [href*=/path/] [attr~=regex]: elements with
 * attribute values that match the regular expression; e.g.
 * img[src~=(?i)\.(png|jpe?g)] : all elements, e.g. *
 * 
 * 
 * el#id: elements with ID, e.g. div#logo el.class: elements with class, e.g.
 * div.masthead el[attr]: elements with attribute, e.g. a[href] Any combination,
 * e.g. a[href].highlight ancestor child: child elements that descend from
 * ancestor, e.g. .body p finds p elements anywhere under a block with class
 * "body" parent > child: child elements that descend directly from parent, e.g.
 * div.content > p finds p elements; and body > * finds the direct children of
 * the body tag siblingA + siblingB: finds sibling B element immediately
 * preceded by sibling A, e.g. div.head + div siblingA ~ siblingX: finds sibling
 * X element preceded by sibling A, e.g. h1 ~ p el, el, el: group multiple
 * selectors, find unique elements that match any of the selectors; e.g.
 * div.masthead, div.logo
 * 
 * Pseudo selectors
 * 
 * :lt(n): find elements whose sibling index (i.e. its position in the DOM tree
 * relative to its parent) is less than n; e.g. td:lt(3) :gt(n): find elements
 * whose sibling index is greater than n; e.g. div p:gt(2) :eq(n): find elements
 * whose sibling index is equal to n; e.g. form input:eq(1) :has(selector): find
 * elements that contain elements matching the selector; e.g. div:has(p)
 * :not(selector): find elements that do not match the selector; e.g.
 * div:not(.logo) :contains(text): find elements that contain the given text.
 * The search is case-insensitive; e.g. p:contains(jsoup) :containsOwn(text):
 * find elements that directly contain the given text :matches(regex): find
 * elements whose text matches the specified regular expression; e.g.
 * div:matches((?i)login) :matchesOwn(regex): find elements whose own text
 * matches the specified regular expression
 * 
 * 
 * 
 * Extract attributes, text, and HTML from elements Problem
 * 
 * After parsing a document, and finding some elements, you'll want to get at
 * the data inside those elements. Solution:
 * 
 * - To get the value of an attribute, use the Node.attr(String key) method -
 * For the text on an element (and its combined children), use Element.text() -
 * For HTML, use Element.html(), or Node.outerHtml() as appropriate
 * 
 * later: specific extractor using "NooLabWebStandards", which knows how to deal
 * with CMS, blogs, news pages (even generic) etc
 * 
 * 
 */
public class TextsExtractor {

	Document soupdoc;
	 
	HTMLParser htmlParser ;
	Sections sections;
	String html;
	
	String plainText;
	

	public TextsExtractor(HTMLParser htmlParser, Document soupdoc) {
		//
		this.soupdoc = soupdoc;
		sections = new Sections();
		html = htmlParser.htmlStr;
		this.htmlParser = htmlParser;
	}

	public Document parse() {
		// first "protect" </br> and <br> by $$CR$$
		// remove tags like b em span by simple replace
		// --> get text from within, remove

		prepareDocument();

		// now get the text
		plainText = soupdoc.text();
		
		return soupdoc;
	}

	private void prepareDocument() {

		removeComments(soupdoc) ;
		
		replaceSimpleDecorations(soupdoc);
		
		removeScripting();
		
		
		// we have to reparse it
		soupdoc = Jsoup.parse(html);
		
		html = soupdoc.html();
		
	}

	private void removeScripting() {
		// 
		
	}

	public void parse(String divIdName) {

	}

	

	private void replaceSimpleDecorations(Document soupdoc) {
		int k=0;
		Element deco;
		String str, cont; 
		
		String[] decorations = new String[]{"b","em","i","a"} ;
		
		
		// loop through all kinds
		for (int d=0;d<decorations.length;d++){
			// get all elements of a kind
			Elements decos = soupdoc.select(decorations[d]);
			
			for (int i=0;i<decos.size();i++){
				// get outerHtml, remove them through replace
				deco = decos.get(i);
				str = deco.outerHtml();
				cont = " "+deco.text()+" ";
				html = htmlParser.strgutil.replaceAll(html, str, cont);
			}// i->
			
		}// d->
		k=0;
	}

	
	//
	private static void removeComments(Node node) {

		for (int i = 0; i < node.childNodes().size(); i++) {
			Node child = node.childNode(i);
			if (child.nodeName().equals("#comment")){
				child.remove();
			}else {
				removeComments(child);
				i++;
			}
		}
	}
}
